/*
Java board game library.
Copyright (C) 2011  Deepesh Garg

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package d.g.boardgames.chess;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AnalysisData {
	public static final float NOT_SET = Float.NEGATIVE_INFINITY;
	private float whiteScore = NOT_SET;
	private float blackScore = NOT_SET;
	private String comment = "";
	private String fen = "";
	private ChessGameData hint;
	
	
	public String getComment() {
		return comment;
	}
	public void setComment(String comment) {
		this.comment = comment;
	}
	public float getBlackScore() {
		return blackScore;
	}
	public void setBlackScore(float blackScore) {
		this.blackScore = blackScore;
	}
	public float getWhiteScore() {
		return whiteScore;
	}
	public void setWhiteScore(float whiteScore) {
		this.whiteScore = whiteScore;
	}
	public String getFen() {
		return fen;
	}

	public void setFen(String fen) {
		this.fen = fen;
	}

	public ChessGameData getHint() {
		return hint;
	}

	public void setHint(ChessGameData hint) {
		this.hint = hint;
	}

	public String getString() {
		String analysisString = "score:\"" + getWhiteScore() + "\"";
		if (comment != null && !comment.trim().equals("")) {
			analysisString += " hint:\"" + comment + "\"";
		}
		if (fen != null && !fen.trim().equals("")) {
			analysisString += " fen:\"" + fen + "\"";
		}
		return analysisString;
	}
	
	public AnalysisData fromString(String analysisString, AnalysisData analysisData) {
		if (analysisData == null) {
			analysisData = this;
		}
		String nameValueExpression = "\\s*([^:]*):\"([^\"]*)\"" ;
		Pattern p = Pattern.compile(nameValueExpression);
		Matcher m = p.matcher(analysisString);
		while (m.find()) {
			if (m.group(1).equals("score")) {
				try {
					analysisData.setWhiteScore(Float.parseFloat(m.group(2)));
					analysisData.setBlackScore(-Float.parseFloat(m.group(2)));
				} catch (NumberFormatException nfe) {}
			}
			if (m.group(1).equals("fen")) {
				analysisData.setFen(m.group(2));
			}
			if (m.group(1).equals("hint")) {
				analysisData.setComment(m.group(2));
			}
		}
		return analysisData;
	}
}
